{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv('iris_dataset.csv')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 150 entries, 0 to 149\n", "Data columns (total 5 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 sepal_length 150 non-null float64\n", " 1 sepal_width 150 non-null float64\n", " 2 petal_length 150 non-null float64\n", " 3 petal_width 150 non-null float64\n", " 4 species 150 non-null object \n", "dtypes: float64(4), object(1)\n", "memory usage: 6.0+ KB\n" ] } ], "source": [ "data.info()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "feature = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']\n", "predection_class = ['species']" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "X = data[feature].values\n", "y = data[predection_class].values" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shape of X_test is (45, 4)\n", "Shape of X_train is (105, 4)\n", "Shape of Y_test is (45, 1)\n", "Shape of Y_train is (105, 1)\n" ] } ], "source": [ "from sklearn.model_selection import train_test_split\n", "X_train, X_test, Y_train, Y_test = train_test_split(X,y,test_size=0.30)\n", "print(f\"Shape of X_test is {X_test.shape}\")\n", "print(f\"Shape of X_train is {X_train.shape}\")\n", "print(f\"Shape of Y_test is {Y_test.shape}\")\n", "print(f\"Shape of Y_train is {Y_train.shape}\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\shubh\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\sklearn\\utils\\validation.py:1111: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n", " y = column_or_1d(y, warn=True)\n" ] } ], "source": [ "from sklearn.neighbors import NearestCentroid\n", "clf = NearestCentroid()\n", "clf.fit(X_train, Y_train)\n", "Y_pred = clf.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy 95.55555555555556\n" ] } ], "source": [ "from sklearn import metrics\n", "print(\"Accuracy\", metrics.accuracy_score(Y_test, Y_pred)*100)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }